class ContactsController < ApplicationController
  before_action :set_contact, only: [:show, :edit, :update, :destroy]

  # GET /contacts
  # GET /contacts.json
  def index
    @contacts = Contact.all
  end

  # GET /contacts/1
  # GET /contacts/1.json
  def show
  end

  # GET /contacts/new
  def new
    @contact = Contact.new
    # @patients = Patient.all
  end

  # GET /contacts/1/edit
  def edit
  	# @patients = Patient.all
  end

  # POST /contacts
  # POST /contacts.json
  def create
    #@contact = Contact.new(contact_params)
    if params.has_key?(:patient_id)
    	@patient = Patient.find(params[:patient_id])
    	@contact = @patient.contacts.create(contact_params)
    else
    	@staff = Staff.find(params[:staff_id])
    	@contact = @staff.contacts.create(contact_params)
    end
    #redirect_to patient_path(@patient)

    respond_to do |format|
      if @contact.save
        if @patient
        	format.html { redirect_to @patient , notice: 'Address was successfully created.' }
        else
        	format.html { redirect_to @staff , notice: 'Address was successfully created.' }
        end
        
        format.json { render :show, status: :created, location: @contact }
      else
        format.html { render :new }
        format.json { render json: @contact.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /contacts/1
  # PATCH/PUT /contacts/1.json
  def update
  	if params.has_key?(:patient_id)
    	@patient = Patient.find(params[:patient_id])
    else
    	@staff = Staff.find(params[:staff_id])
    end
    respond_to do |format|
      if @contact.update(contact_params)
      	if @patient
        	format.html { redirect_to @patient, notice: 'Address was successfully updated.' }
        else
        	format.html { redirect_to @staff, notice: 'Address was successfully updated.' }
        end
        format.json { render :show, status: :ok, location: @contact }
      else
        format.html { render :edit }
        format.json { render json: @contact.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /contacts/1
  # DELETE /contacts/1.json
  def destroy
  	if params.has_key?(:patient_id)
  		@patient = Patient.find(params[:patient_id])
  	else
  		@staff = Staff.find(params[:staff_id])
  	end
    @contact.destroy
    respond_to do |format|
    	if @patient
      		format.html { redirect_to @patient, notice: 'Detail address was successfully destroyed.' }
      	else
      		format.html { redirect_to @staff, notice: 'Detail address was successfully destroyed.' }
      	end
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_contact
      @contact = Contact.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def contact_params
      params.require(:contact).permit(:contact_type_id, :address, :postal_code, :phone, :fax, :created_at, :updated_at, :created_by, :updated_by, :patient_ids => [])
    end
end
